<template>
  <el-drawer v-model="drawerVisible" :destroy-on-close="true" size="450px" :title="`${drawerProps.title}MBOM`">
    <template #header>
      <h4>{{ drawerProps.title }}MBOM</h4>
    </template>
    <el-form
      ref="ruleFormRef"
      label-width="100px"
      label-suffix=" :"
      :rules="rules"
      :disabled="drawerProps.isView"
      :model="drawerProps.row"
      :hide-required-asterisk="drawerProps.isView"
    >
      <!-- 同步模式特有字段 -->
      <template v-if="drawerProps.isSync">
        <el-form-item label="EBOM ID" prop="ebomId">
          <el-input-number v-model="drawerProps.row.ebomId" placeholder="请输入EBOM ID" :min="1" />
        </el-form-item>
        <el-form-item label="EBOM版本" prop="ebomVersion">
          <el-input v-model="drawerProps.row.ebomVersion" placeholder="请输入EBOM版本" clearable />
        </el-form-item>
        <el-form-item label="产品ID" prop="productId">
          <el-input-number v-model="drawerProps.row.productId" placeholder="请输入产品ID" :min="1" />
        </el-form-item>
      </template>

      <!-- 普通创建/编辑模式字段 -->
      <template v-else>
        <el-form-item label="MBOM编码" prop="mbomCode">
          <el-input v-model="drawerProps.row.mbomCode" placeholder="请输入MBOM编码" clearable />
        </el-form-item>
        <el-form-item label="MBOM名称" prop="mbomName">
          <el-input v-model="drawerProps.row.mbomName" placeholder="请输入MBOM名称" clearable />
        </el-form-item>
        <el-form-item label="产品ID" prop="productId">
          <el-input-number v-model="drawerProps.row.productId" placeholder="请输入产品ID" :min="1" />
        </el-form-item>
        <el-form-item label="版本" prop="version">
          <el-input v-model="drawerProps.row.version" placeholder="请输入版本号" clearable />
        </el-form-item>
      </template>

      <el-form-item label="描述" prop="description">
        <el-input
          v-model="drawerProps.row.description"
          placeholder="请输入描述"
          type="textarea"
          :rows="4"
          clearable
        />
      </el-form-item>
    </el-form>
    <template #footer>
      <el-button @click="drawerVisible = false">取消</el-button>
      <el-button v-show="!drawerProps.isView" type="primary" @click="handleSubmit">确定</el-button>
    </template>
  </el-drawer>
</template>

<script setup lang="ts" name="MBOMDrawer">
import { ref, reactive } from "vue";
import { ElMessage, FormInstance } from "element-plus";
import { MBOM } from "@/api/modules/mbom";

const rules = reactive({
  ebomId: [{ required: true, message: "请输入EBOM ID" }],
  ebomVersion: [{ required: true, message: "请输入EBOM版本" }],
  mbomCode: [{ required: true, message: "请输入MBOM编码" }],
  mbomName: [{ required: true, message: "请输入MBOM名称" }],
  productId: [{ required: true, message: "请输入产品ID" }],
  version: [{ required: true, message: "请输入版本号" }]
});

interface DrawerProps {
  title: string;
  isView: boolean;
  isSync?: boolean;
  row: Partial<MBOM.MBOMListItem & MBOM.CreateMBOMRequest & MBOM.SyncMBOMFromEBOMRequest>;
  api?: (params: any) => Promise<any>;
  getTableList?: () => void;
}

const drawerVisible = ref(false);
const drawerProps = ref<DrawerProps>({
  isView: false,
  title: "",
  row: {}
});

// 接收父组件参数
const acceptParams = (params: DrawerProps) => {
  drawerProps.value = params;
  drawerVisible.value = true;
};

// 提交数据（新增/编辑）
const ruleFormRef = ref<FormInstance>();
const handleSubmit = () => {
  ruleFormRef.value!.validate(async valid => {
    if (!valid) return;
    try {
      await drawerProps.value.api!(drawerProps.value.row);
      ElMessage.success({ message: `${drawerProps.value.title}MBOM成功！` });
      drawerProps.value.getTableList!();
      drawerVisible.value = false;
    } catch (error) {
      console.error(error);
      ElMessage.error({ message: `${drawerProps.value.title}MBOM失败！` });
    }
  });
};

defineExpose({
  acceptParams
});
</script>